<?php
namespace app\atxunda\model;
use think\Model;
use think\Input;
use think\Cookie;
use think\Db;

class Course extends Model
{
	public function disposeData($datas){
		if($datas['status'] == 'select'){
			$query = array('status'=>'select');
			$where = [];
			if($datas['nav_id']){
				$query['nav_id'] = $datas['nav_id'];
				$where['c.nav_id'] = $datas['nav_id'];
			}
			if($datas['type_id']){
				$query['type_id'] = $datas['type_id'];
				$where['c.type_id'] = $datas['type_id'];
			}
			if($datas['s_id']){
				$query['s_id'] = $datas['s_id'];
				$where['c.s_id'] = $datas['s_id'];
			}
			if($datas['is_show']){
				$query['is_show'] = $datas['is_show'];
				$where['s.s_show'] = $datas['is_show'];
			}
			if($datas['keywords']){
				$query['keywords'] = $datas['keywords'];
				$keywords = trim($datas['keywords']);
				$where['course_name'] = ['like',"%$keywords%"];
			}
			if($datas['vid']){
				$query['vid'] = $datas['vid'];
				$vid = trim($datas['vid']);
				$where['vid'] = $vid;
			}
			$admin = unserialize(cookie('admin'));
			/*if($admin['admin_id'] != 1){
				$where['a.admin_id'] = $admin['admin_id'];
			}*/
			if($admin['role_id'] != 1) $where['c.nav_id'] = ['in',$admin['nav_id']];
			$query['status'] = 'select';
			$arr = self::getAllData($where, $query);
			$page = isset($datas['page']) ? $datas['page'] : 1;
			unset($where['s.s_show']);
			$data['status'] = '';
			$data['arr'] = $arr;
			$data['testIds'] = $testIds;
		}else if($datas['status'] == 'del'){
			$course_ids = $datas['course_id'];
			$course_id = implode(',', $course_ids);
			$datass = self::where('course_id','in',$course_id)->select();
			$del = self::where('course_id','in',$course_id)->delete();
			if($del){
				$data['status'] = 'del';
			}else{
				$data['status'] = 0;
			}
		}else if($datas['status'] == 'sort'){
			$course_ids = $datas['course_id'];
			$course_sorts = $datas['course_sort'];
			if($course_ids){
				$datas['sort_num'] = $datas['sort_num'] ?: 0;
				foreach ($course_ids as $key => $id) {
					$up = self::where('course_id',$id)->update(['course_sort'=>($course_sorts[$id]-$datas['sort_num'])]);
				}
				$data['status'] = 'sort';
				if($up){
					$data['nav_id'] = $datass[0]['nav_id'];
					$data['type_id'] = $datass[0]['type_id'];
				}
			}
		}else if($datas['status'] == 'must'){
			$course_ids = $datas['course_id'];
			$must = $datas['changemust'];
			if($course_ids){
				self::where('course_id','in',$course_ids)->update(['is_must'=>$must]);
			}
			$data['status'] = 'must';
		}else if($datas['status'] == 'ad'){
			$course_ids = $datas['course_id'];
			$ad = $datas['changead'];
			if($course_ids){
				self::where('course_id','in',$course_ids)->update(['is_ad'=>$ad]);
			}
			$data['status'] = 'must';
		}else{
			$course_ids = $datas['course_id'];
			$sCopyId = $datas['sCopyId'];
			if($course_ids){
				$courseDatas = self::where(['course_id'=>['in',$course_ids]])->select();
				$sData = Db::table('specific')->field('type_id,nav_id')->where('s_id',$sCopyId)->find();
				$courseDatas = json_decode(json_encode($courseDatas),true);
				foreach ($courseDatas as $key => $v) {
					unset($courseDatas[$key]['course_id']);
					$courseDatas[$key]['s_id'] = $sCopyId;
					$courseDatas[$key]['type_id'] = $sData['type_id'];
					$courseDatas[$key]['nav_id'] = $sData['nav_id'];
				}
				$model = new Course;
				$model->saveAll($courseDatas);
			}
			$data['status'] = 'copy';
		}
		return $data;
	}

	public function getAllData($where, $query){
		return self::field('c.*,t.type_name,n.nav_name,s.s_name,a.admin_name')->alias('c')->join('type t','t.type_id = c.type_id')->join('nav n','n.nav_id = c.nav_id')->join('specific s','s.s_id = c.s_id','left')->join('admin a','a.admin_id = c.updated_admin_id')->where($where)->order('course_sort desc')->paginate(20,false, ['query' => $query]);
	}

}